package com.yu.controller;

import cn.hutool.http.server.HttpServerRequest;
import cn.hutool.http.server.HttpServerResponse;
import com.yu.judge.model.JudgeInfo;
import com.yu.judge.service.JudgeService;
import com.yu.judge.service.JudgeService_test;
import com.yu.model.entity.QuestionSubmit;
import com.yu.model.pojo.Result;
import com.yu.model.vo.JudgeInfoVo;
import com.yu.model.vo.QuestionSubmitVo;
import com.yu.service.QuestionSubmitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

@RestController
@CrossOrigin
public class QuestionSubmitController {


    @Resource
    private QuestionSubmitService questionSubmitService;

    @Resource
    private JudgeService judgeService;

//    @RequestMapping("/test")
//    public String print(){
//        judgeService.doJudge(1L);
//        return "hell0";
//    }


    /**
     * 测试
     *
     * @return
     */
    @Resource
    private JudgeService_test judgeService_test;

    @RequestMapping("/test2")
    public Result print2() {
        return Result.success();
    }


/*

  第一版

    @PostMapping("/test3")
    public Result test3(@RequestBody QuestionSubmit questionSubmit){

        QuestionSubmit judgeRes = judgeService.doJudge(questionSubmitService.add(questionSubmit).getId());


        if(judgeRes.getStatus().equals(2)){
            return Result.success();
        }

        return Result.error(judgeRes.getJudgeInfo());
    }
*/


    @PostMapping("/test3")
    public Result test3(@RequestBody QuestionSubmit questionSubmit) {


        JudgeInfo doJudge = judgeService_test.doJudge(questionSubmitService.add(questionSubmit).getId());

        JudgeInfoVo judgeInfoVo = new JudgeInfoVo();
        judgeInfoVo.setTime(doJudge.getTime());
        judgeInfoVo.setMemory(doJudge.getMemory());
        judgeInfoVo.setCodeSize(doJudge.getCodeSize());

        if (doJudge.getSandboxMessage().equals("编译失败")) {
            return Result.error(doJudge.getSandboxMessage() + " " + doJudge.getMessage(), judgeInfoVo);
        }

        if (doJudge.getMessage().equals("运行成功")) {
            return Result.success(judgeInfoVo);
        }

        return Result.error(doJudge.getSandboxMessage() + " " + doJudge.getMessage(), judgeInfoVo);
    }

}
